/**
 * @description 定义全局助手类方法
 */
import { SCRECT_KEY } from '@/config/common.conf'
import { AES, MD5, enc } from 'crypto-js'

/**
 * 对称加密方法
 * @param {String} msg 需要加密的字符串
 * @returns String 加密后的数据
 */
export function enCodeApi(msg) {
    return AES.encrypt(msg, SCRECT_KEY).toString()
}

export function deCodeApi(msg) {
    return AES.decrypt(msg, SCRECT_KEY).toString(enc.Utf8)
}

export function md5Api(msg) {
    return MD5(msg).toString()
}

/**
 * 
 * @param {Function} cb 结果回调方法
 * @param {Boolean} multiple 是否允许多选
 * @param {String} accept 选择文件的类型
 */
export function filePicker(cb, multiple = true,accept = 'image/*' ){
    let input = document.createElement('input')
    input.setAttribute('type','file') 
    input.setAttribute('accept',accept)
    if(multiple) input.setAttribute('multiple','multiple')
    input.onchange=function(){
        let file = this.files
        if(!!cb&&Object.prototype.toString.call(cb)==='[object Function]') cb(file)
    }
    input.click()
}